﻿package src {
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import src.uipackage.ui.events.ValueSliderEvent;
	import src.uipackage.ui.OBO_ValueSlider;
	
	/**
	 * Class for the settings tab UI and functions
	 * @author Indrek Tamming
	 */
	public class SettingsTab extends Tab {
		
		/**
		 * Callback to the main class Viewer
		 */
		private var cb:Viewer;
		
		/**
		 * Label for the slider. Global variable to allow fast change.
		 */
		private var alphaSliderLabel:TextField;
		
		/**
		 * SettingsTab constructor
		 * @param	cb	-	callback to Viewer
		 */
		public function SettingsTab(cb:Viewer):void {
			this.cb = cb;
		}
		
		/**
		 * Draw the settings tab according to the parameters got by the callback
		 */
		public function draw():void {
			while (this.numChildren > 0) this.removeChildAt(0);
			var leftX:int = -7;
			
			addChild(drawButton(leftX, 10, exportBtnClicked));
			addChild(drawLabel("Export to PNG", 10, 6));
			
			addChild(drawButton(leftX, 40, hideShowNamesClicked));
			addChild(drawLabel((cb.seqRowNamesVisible?"Hide ":"Show ") + "seq names", 10, 36));
			
			alphaSliderLabel = drawLabel("Match transparency " + String((1 - cb.innerAlpha) * 100 >> 0) + "%", leftX, 220);
			addChild(alphaSliderLabel);
			var alphaSlider:OBO_ValueSlider = new OBO_ValueSlider(130, 1, 0, cb.innerAlpha);
			alphaSlider.x = leftX;
			alphaSlider.y = 235;
			alphaSlider.addEventListener(ValueSliderEvent.DRAG, alphaSliderDrag);
			alphaSlider.addEventListener(ValueSliderEvent.STOPDRAG, alphaSliderStopDrag);
			addChild(alphaSlider);
			
			// Motif height type, TODO
			
		}
		
		/**
		 * Export button click event handler
		 * @param	e	-	CLICK event
		 */
		private function exportBtnClicked(e:MouseEvent = null):void {
			cb.exportView();
		}
		
		/**
		 * Hide/show names button click event handler
		 * @param	e	-	CLICK event
		 */
		private function hideShowNamesClicked(e:MouseEvent = null):void {
			if (cb.seqRowNamesVisible) cb.seqRowNamesVisible = false;
			else cb.seqRowNamesVisible = true;
			cb.viewIsDirty = true;
			cb.uiIsDirty = true;
		}
		
		/**
		 * Temp function for tracing inner alpha change
		 */
		private function innerAlphaChanged(e:Event):void {
			trace(e.toString);
		}
		
		/**
		 * Alpha slider stop drag event handler
		 * @param	e	-	the ValueSliderEvent
		 */
		private function alphaSliderStopDrag(e:ValueSliderEvent):void {
			cb.innerAlpha = e.value;
			cb.uiIsDirty = true;
			cb.viewIsDirty = true;
		}
		
		/**
		 * Alpha slider dragging event handler
		 * @param	e	-	the ValueSliderEvent
		 */
		private function alphaSliderDrag(e:ValueSliderEvent):void {
			cb.innerAlpha = e.value;
			alphaSliderLabel.text = "Match transparency " + String((1 - cb.innerAlpha) * 100 >> 0) + "%";
		}
	}
	
}